<?php
namespace Home\Controller;
use OSS\Core\OssException;
use OSS\OssClient;
class MockaudioController extends CommonController
{
	public function _initialize()
	{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
		$this->Is_power("show");        //检测用户权限,如果错误直接返回错误,终止程序
		$this->assign('heardadminData',session('heardadminData'));
	}
	// 自媒体列表
	public function mocklist()
	{
		$Classmedia = D('Classmedia');
		$data = $Classmedia->GetMedialist();
		$Page = new \Think\Page(count($data),10);
        $this->page = $Page->show();
        $this->data = array_slice($data,$Page->firstRow,$Page->listRows);
		$this->display();
	}

	// 分类列表
	public function mockclass()
	{
		$Classmedia = D('Classmedia');
		$data = $Classmedia->GetClass();
		$this->data = $data;
		$this->display();
	}

	// 添加分类
	public function mockclassadd()
	{
		$this->display();
	}

	// 新增自媒体
	public function mockadd()
	{
		$Classmedia = D('Classmedia');
		$data = $Classmedia->GetClass();
		$this->data = $data;
		$this->display();
	}

	// 修改自媒体
	public function mockclassedit()
	{
		$Classmedia = D('Classmedia');
		$id = I('get.id');
		$data = $Classmedia->mockclassedit($id);
		$this->data = $data;
		$this->display();
	}

	// 修改自媒体提交
	public function mockclasseditpost()
	{
		$Classmedia = D('Classmedia');
		$name = I('post.name');
		$id = I('post.id');
		$result = $Classmedia->mockclasseditpost($id,$name);
	    if ($result) {
	        $this->success('修改成功');
	    } else {
	    	$this->error('修改失败');
	    }
	}

	// 添加分类提交
	public function mockclassaddpost()
	{
		$Classmedia = D('Classmedia');
		$name = I('post.name');
		$result = $Classmedia->mockclassaddpost($name);
	    if ($result) {
	        $this->success('新增成功');
	    } else {
	    	$this->error('新增失败');
	    }
	}

	// 自媒体修改
	public function mockedit()
	{
		$id = I('get.id');
		$Classmedia = D('Classmedia');
		$data = $Classmedia->GetMediafind($id);
		$class = $Classmedia->GetClass();
		foreach ($class as &$vo) {
			if($vo['id'] == $data['cid'] ){
				$vo['selected'] = "selected='selected'";
			}
		}
		$this->data = $data;
		$this->class = $class;
		$this->display();
	}

	// 查看自媒体对应音频
	public function mockaudiolist()
	{
		$Classmedia = D('Classmedia');
		$id = I('get.id');
		$data = $Classmedia->GetMockAudioList($id);
		$this->data = $data;
		$this->display();
	}

	// 给自媒体新增音频
	public function mockaudioadd()
	{
		$Classmedia = D('Classmedia');
		$id = I('get.id');
		$data = $Classmedia->GetMediafind($id);
		$this->data = $data;
		$this->display();
	}

	// 修改单个自媒体音频
	public function saveaudiofind()
	{
		$Classmedia = D('Classmedia');
		$id = I('get.id');
		$data = $Classmedia->GetMockAudiofind($id);
		$data['wz'] = html_entity_decode($data['wz']);
		$this->data = $data;
		$this->display();

	}

	// 新增自媒体提交
	public function mockpost()
	{
		$Classmedia = D('Classmedia');
		if(IS_POST){
		    $upload = new \Think\Upload();
		    $upload->maxSize   =  3145728 ;
		    $upload->exts      =  array('jpg', 'gif', 'png', 'jpeg');
		    $upload->rootPath  =  './Uploads/mock/';
		    $info = $upload->uploadOne($_FILES['photo']);
		    if(!$info) {
		        $this->error($upload->getError());
		    }else{
	    	    $FilePath ='/Uploads/mock/'.$info['savepath'].$info['savename'];
	    	    $pics = $this->uploadFile('.'.$FilePath);
    	    	$data['pic'] = 'http://heard-img.oss-cn-beijing.aliyuncs.com/'.$pics;
	    	    $data['name'] = $_POST['name'];
	    	    $data['cid'] = $_POST['class'];
	    	    $data['articles'] = $_POST['articles'];
	    	    $data['message'] = $_POST['message'];
	    	    $data['time'] = time();
	    	    $result = $Classmedia->MediaAdd($data);
	    	    if ($result) {
			        $this->success('新增成功');
	    	    } else {
	    	    	$this->error('新增失败');
	    	    }
		    }
		}
	}

	// 修改自媒体提交
	public function mockeditpost()
	{
		$Classmedia = D('Classmedia');
		$id = $_POST['id'];
		if(IS_POST){
		    $upload = new \Think\Upload();
		    $upload->maxSize   =  3145728 ;
		    $upload->exts      =  array('jpg', 'gif', 'png', 'jpeg');
		    $upload->rootPath  =  './Uploads/mock/';
		    $info = $upload->uploadOne($_FILES['photo']);
		    if($info) {
	    	    $FilePath = '/Uploads/mock/'.$info['savepath'].$info['savename'];
		    	$pics = $this->uploadFile('.'.$FilePath);
    	    	$data['pic'] = 'http://heard-img.oss-cn-beijing.aliyuncs.com/'.$pics;
		    }
		    $data['name'] = $_POST['name'];
    	    $data['cid'] = $_POST['class'];
    	    $data['articles'] = $_POST['articles'];
    	    $data['message'] = $_POST['message'];
    	    $data['time'] = time();
    	    $result = $Classmedia->Mediaeidt($id,$data);
    	    if ($result) {
		        $this->success('修改成功');
    	    } else {
    	    	$this->error('失败');
    	    }
		}
	}

	// OSS上传本地文件
    function uploadFile($filePath,$bucket = 'heard-img')
    {
        vendor('aliyun.autoload');
        $accessKeyId = "LTAIIpe6DwZznQ3t";//去阿里云后台获取秘钥
        $accessKeySecret = "hHycAKcsPcnHRKBqMaEQwnvC5Qkosa";//去阿里云后台获取秘钥
        $endpoint = "oss-cn-beijing.aliyuncs.com";//你的阿里云OSS地址
        $object = md5(time().rand(1000,9999));
        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $content = $ossClient->uploadFile($bucket, $object, $filePath);
            return $object;
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
    }

	// 提交新增音频
	public function mockaudioaddpost()
	{
		$Classmedia = D('Classmedia');
		$data['title'] = I('post.title');
		$data['url2']  = I('post.url2');
		$data['wz']    = I('post.html');
		$data['mid']   = I('post.mid');
		$data['rid']   = (int)($data['mid'] . '0000') + rand(0,9999);
		$result = $Classmedia->AddMockaudiopost($data);
		if ($result) {
			$this->ApiSuccess('操作成功');
		} else {
			$this->ApiError('提交失败','999999');
		}
	}

	// 修改音频
	public function mockaudiosavepost()
	{
		$Classmedia = D('Classmedia');
		$data['title'] = I('post.title');
		$data['url2']  = I('post.url2');
		$data['wz']    = I('post.html');
		$id	= I('post.id');
		$result = $Classmedia->SaveMockaudiopost($id,$data);
		if ($result) {
			$this->ApiSuccess('操作成功');
		} else {
			$this->ApiError('提交失败','999999');
		}
	}


	// 删除自媒体
	public function delmedia()
	{
		$id = I('get.id');
		if(!empty($id)){
			$result = M('classmedia')->where(array('id'=>$id))->delete();
		}
		if ($result) {
			$this->success('删除成功', '/Home/Mockaudio/mocklist');
		} else {
			$this->error('删除成功', '/Home/Mockaudio/mocklist');
		}
	}

	// 删除分类
	public function delclass()
	{
		$id = I('get.id');
		if(!empty($id)){
			if(M('classmedia')->where(array('cid'=>$id))->find()!=false){
				$this->error('删除失败 该分类下有自媒体', '/Home/Mockaudio/mockclass');
			}
			$result = M('classify')->where(array('id'=>$id))->delete();
		}
		if ($result) {
			$this->success('删除成功', '/Home/Mockaudio/mocklist');
		} else {
			$this->error('删除成功', '/Home/Mockaudio/mocklist');
		}
	}

	// 删除自媒体单个音频
	public function delaudiofind()
	{
		$id = I('get.id');
		$result =  M('zsmedia')->where(array('id'=>$id))->delete();
		if ($result) {
			$this->success('删除成功', '/Home/Mockaudio/mocklist');
		} else {
			$this->error('删除成功', '/Home/Mockaudio/mocklist');
		}
	}

}